package com.atguigu.gmall.activity.consumer;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.activity.entity.SeckillOrder;
import com.atguigu.gmall.activity.entity.UserRecode;
import com.atguigu.gmall.activity.mapper.SeckillMapper;
import com.atguigu.gmall.common.config.RabbitService;
import com.atguigu.gmall.model.activity.SeckillGoods;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 秒杀下单的监听类
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月16日 18:58
 * @ClassName:SeckillOrderAddConsumer
 * @Description: TODO
 */
@Component
public class SeckillOrderAddConsumer {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private RabbitService rabbitService;

    /**
     * 监听排队消息进行秒杀下单
     */
    @RabbitListener(queues = "seckill_queue")
    public void addOrder(String msg){
        //将消息的类型转换为排队信息
        UserRecode userRecode = JSONObject.parseObject(msg, UserRecode.class);
//        //进行校验，用户是否有为支付的秒杀订单
//        Object o = redisTemplate.boundHashOps("SeckillOrder").get(userRecode.getUsername());
//         if(o!=null){
//             //修改排队的状态
//             userRecode.setStatus(3);
//             //更新用户的排队状态
//             redisTemplate.boundValueOps("SeckillStatus_"+userRecode.getUsername()).set(userRecode);
//
//             return;
//         }
         //校验商品
        String time = userRecode.getTime();
        String goodsId = userRecode.getGoodsId();
        SeckillGoods seckillGoods = (SeckillGoods)redisTemplate.boundHashOps("SeckillGoods_" + time).get(goodsId);
        if(seckillGoods!=null){
          //商品的类型转换
//            Integer stockCount = seckillGoods.getStockCount();
            Object o = redisTemplate.boundListOps("SeckillGoodsQueue_" + goodsId).rightPop();
            //判断库存
            if(o!=null){
                //构建订单信息
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(UUID.randomUUID().toString().replace("-", ""));
                seckillOrder.setGoodsId(goodsId);
                seckillOrder.setNum(userRecode.getNum());
                seckillOrder.setMoney(userRecode.getNum() * seckillGoods.getCostPrice().doubleValue()+"");
                seckillOrder.setStatus("0");
                //将订单信息存入redis
                redisTemplate.boundHashOps("SeckillOrder_"+userRecode.getUsername()).put(seckillOrder.getId(), seckillOrder);;
                //更新库存，存在超卖问题---TODO
                int i = seckillGoods.getStockCount() - userRecode.getNum();
                seckillGoods.setStockCount(i);
//                Long stockNum = redisTemplate.boundHashOps("SeckillGoodsStockNum").increment(goodsId, -1);
//                seckillGoods.setStockCount(stockNum.intValue());
                if(i>0){
                    redisTemplate.boundHashOps("SeckillGoods_"+time).put(goodsId,seckillGoods);
                }else{
                    //售罄处理,将redis中商品的数据清除掉
                    redisTemplate.boundHashOps("SeckillGoods_" + time).delete(goodsId);
                    //将商品的数据同步到数据库中去
                    seckillMapper.updateById(seckillGoods);
                }
                 //更新一下排队的状态
                userRecode.setStatus(2);
                userRecode.setMoney(Float.valueOf(seckillOrder.getMoney()));
                userRecode.setOrderId(seckillOrder.getId());
                //更新用户的排队状态
                redisTemplate.boundValueOps("SeckillStatus_"+userRecode.getUsername()).set(userRecode);
                //发送一个延迟消息,记录订单的有效期倒计时-----TODO
                rabbitService.sendMessage("seckill_dead_exchange",
                        "seckill.dead.order",JSONObject.toJSONString(seckillOrder),"300");

                //结束
                 return;
            }
        }
        //商品售罄了,修改排队的状态，下单失败
        userRecode.setStatus(3);
        //更新用户的排队状态
        redisTemplate.boundValueOps("SeckillStatus_"+userRecode.getUsername()).set(userRecode);
        //重复排队标识删除
        redisTemplate.delete("SeckillUserQueue_" + userRecode.getUsername());
        return;
    }
}
